package com.lkd.job;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.lkd.config.TopicConfig;
import com.lkd.contract.SupplyChannel;
import com.lkd.contract.SupplyContract;
import com.lkd.dao.ChannelDao;
import com.lkd.emq.MqttProducer;
import com.lkd.entity.ChannelEntity;
import com.lkd.service.VendingMachineService;
import com.lkd.vo.VmVO;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-07-20 18:35
 */
@Component
@Slf4j
public class SupplyTaskJob {

    @Autowired
    private ChannelDao channelDao;

    @Autowired
    private MqttProducer producer;

    @Autowired
    private VendingMachineService vendingMachineService;

    //自动创建补货工单
    @XxlJob("supplyTask")
    public void supplyTask(){
        int shardTotal = XxlJobHelper.getShardTotal();//总分片数 服务数量 3
        int shardIndex = XxlJobHelper.getShardIndex();//这一个服务实例编号 0 1 或 2

        //1.查询出所有需要补货的售货机货道信息
        List<ChannelEntity> supplyChannel = channelDao.getSupplyChannel(shardTotal,shardIndex);

        if(CollectionUtils.isEmpty(supplyChannel)){
            return;
        }

        //获取所有innercode 售货机编号
        List<String> collect =
                supplyChannel.stream().map(ChannelEntity::getInnerCode).distinct().collect(Collectors.toList());

        if(collect.size() > 0){
            System.out.println("售货机" + collect.get(0) + "由当前服务实例" + shardIndex + "处理");
        }

        //遍历所有售货机编号，获取当前售货机编号下货道数据
        for (String innercode : collect) {
            List<ChannelEntity> list =
                    supplyChannel.stream().filter(data -> data.getInnerCode().equals(innercode)).collect(Collectors.toList());

            //2.封装协议类
            SupplyContract supplyContract = new SupplyContract();
            supplyContract.setInnerCode(innercode);

            VmVO vmVO = vendingMachineService.findByInnerCode(innercode);

            if(vmVO == null){
                continue;
            }

            supplyContract.setRegionId(vmVO.getRegionId());

            List<SupplyChannel> contractList =  new ArrayList<>();
            supplyContract.setSupplyData(contractList);

            for (ChannelEntity channelEntity : list) {
                SupplyChannel supplyChannel1 = new SupplyChannel();
                supplyChannel1.setChannelId(channelEntity.getChannelCode());
                supplyChannel1.setCapacity(channelEntity.getMaxCapacity() - channelEntity.getCurrentCapacity());
                supplyChannel1.setSkuId(channelEntity.getSkuId());
                supplyChannel1.setSkuName(channelEntity.getSku().getSkuName());
                supplyChannel1.setSkuImage(channelEntity.getSku().getSkuImage());
                contractList.add(supplyChannel1);
            }

            //3.发送数据到emq
            try {
                producer.send(TopicConfig.TASK_SUPPLY_TOPIC,2,supplyContract);
            } catch (JsonProcessingException e) {
                log.error("自动创建工单消息类失败",e);
            }
        }

    }
}
